const fs = require('fs');
const path = require('path');
module.exports = class extends baseLogic {
    constructor() {
        super();
    }



    async getItems(_rootPath, isCurDir, ext) {
        let _files = [];
        //if(_rootPath==='') return null;
        let rootPath = path.join(global.RootPath, _rootPath);
        return new Promise((resolve, reject) => {
            fs.readdir(rootPath, {
                encoding: 'utf8'
            }, (err, files) => {
                if (err) {
                    reject(err);
                } else {
                    files.map(item => {
                        if (!item.startsWith('_')) {
                            if (fs.statSync(path.join(rootPath, item)).isDirectory()) {
                                let model = {
                                    label: item,
                                    title: item,
                                    value: item,
                                    path: _rootPath + "/" + item,
                                    type: 'dir'
                                };
                                if (!isCurDir) {
                                    let isHasChildren = false;
                                    log.debug(path.join(global.RootPath, model.path));
                                    let children = fs.readdirSync(path.join(global.RootPath, model.path));
                                    for (let i = 0; i < children.length; i++) {
                                        let it = children[i];
                                        if (fs.statSync(path.join(global.RootPath, model.path, it)).isDirectory()) {
                                            isHasChildren = true;
                                            break;
                                        } else {
                                            let _str = children[i].split('.');
                                            let curFileExt = _str[_str.length - 1];
                                            if (ext.indexOf(curFileExt.toLowerCase()) > -1) {
                                                isHasChildren = true;
                                                break;
                                            }
                                        }

                                    }

                                    if (isHasChildren) {
                                        model.children = [];
                                        model._children = [];
                                        model.loading = false;
                                    }

                                }
                                _files.push(model);
                            } else {
                                if (ext && ext !== '') {
                                    let _str = item.split('.');
                                    let curFileExt = _str[_str.length - 1];
                                    if (ext.indexOf(curFileExt.toLowerCase()) > -1) {
                                        _files.push({
                                            label: item,
                                            title: item,
                                            value: item,
                                            path: _rootPath + "/" + item,
                                            type: 'file'
                                        })
                                    }
                                } else {
                                    _files.push({
                                        label: item,
                                        title: item,
                                        value: item,
                                        path: _rootPath + "/" + item
                                    })
                                }


                            }
                        }

                    });
                    resolve(_files);
                }



            });

        });

    }

    /**
     * 获取目录以及文件
     * @param rootPath
     * @returns {Promise<boolean>}
     */
    async getAllFile(_rootPath) {
        let _files = [];
        if (_rootPath === '') return null;
        let rootPath = global.RootPath + _rootPath;
        return new Promise((resolve, reject) => {
            fs.readdir(rootPath, {
                encoding: 'utf8'
            }, (err, files) => {
                if (err) {
                    reject(err);
                } else {
                    files.forEach(async item => {
                        if (item.indexOf('.') > -1) {
                            _files.push({
                                label: item,
                                value: item,
                                children: await this.getAllFile(_rootPath + "/" + item)
                            })
                        } else {
                            _files.push({
                                label: item,
                                value: item
                            })
                        }
                    });
                    resolve(_files);
                }
            });

        })

    }


};